import VueRouter from "vue-router";
import routes from "./routes";
import Vue from "vue";
import store from "../store";
Vue.use(VueRouter);
const router = new VueRouter({
  routes,
  mode: "history",
});

router.beforeEach((to, from, next) => {
  console.log(to);
  if (to.meta.auth) {
    // 需要鉴权
    const status = store.getters["loginUser/status"];
    if (status === "loading") {
      // 加载中
      next({
        path: "/loading",
        query: {
          returnurl: to.fullPath,
        },
      });
    } else if (status === "login") {
      // 已经登录
      next();
    } else {
      alert("您还未有登录，请登录后再进入该页面");
      // 没有登录
      next({
        path: "/login",
        query: {
          returnurl: to.fullPath,
        },
      });
    }
  } else {
    next();
  }
});

export default router;
